#include <cctype>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ifstream;
using std::ofstream;

class Record{
public:
    Record(const string & word, int frequency)
    :_word(word)
    ,_frequency(frequency)
    {
    }
    friend class Dictionary;
private:
    string _word;
    int _frequency;
};

class Dictionary{
public:
    Dictionary(int size){
        _dict.reserve(size);
    }
    bool is_word(string word){
        for(char & ch:word){
            if(isalpha(ch) == 0){
                return false;
            }
        }
        return true;
    }
    void read(const string &filename){
        cout << "start read!"<<endl;
        ifstream ifs(filename);
        cout << ifs.good()<<endl;
        string word;
        while(ifs >> word){
            if(is_word(word)){
                bool have_find = false;
                auto it = _dict.begin();
                while(it != _dict.end()){
                    if(it->_word == word){
                        (it->_frequency)++;
                        have_find = true;
                        break;
                    }
                    ++it;
                }
                if(have_find == false){
                    _dict.emplace_back(word,1);
                } 
            }
            word.clear();

        }
    }

    void store(const string &filename){
        ofstream ofs(filename);
        for(auto & re:_dict){
            ofs << re._word <<" "<< re._frequency << endl;
        }
    }
private:
    vector<Record> _dict;
};

void test(){
    Dictionary dic(10000);
    dic.read("The_Holy_Bible.txt");
    //dic.read("test.txt");
    dic.store("Bible.data");
}

int main()
{
    test ();
    return 0;
}

